﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.Cache
{
    public class CacheUtils
    {
        /// <summary>
        /// 缓存处理
        /// </summary>
        public static bool FileCacheProgress()
        {
            return FileCacheProgress(false, true);
        }
        /// <summary>
        /// 重写缓存
        /// </summary>
        public static void FileCacheRewrite()
        {
            FileCacheProgress(true, false);
        }
        /// <summary>
        /// 仅清除首页和列表页面的缓存
        /// </summary>
        public static void RemoveCache()
        {
 
        }
        /// <summary>
        /// 清除全部缓存
        /// </summary>
        public static void RemoveAllCache()
        {
            DataTable Dt = FileUtils.GetAllFolder("~/Config/Cache/CacheFiles/", "*.cache");
            foreach (DataRow Row in Dt.Rows)
            {
                FileUtils.DeleteFile(HttpContext.Current.Server.MapPath(Row["FileName"].ToString()));
            }
        }
        /// <summary>
        /// 缓存处理
        /// </summary>
        /// <param name="IsReCache">true强行重写cache</param>
        /// <param name="IsRewritePage">是否重写输入出页面内容</param>
        /// <returns></returns>
        private static bool FileCacheProgress(bool IsReCache, bool IsRewritePage)
        {
            FileCaches filecaches = new FileCaches();
            try
            {
                filecaches.FileCacheMiniutes = Convert.ToInt32(UIConfig.CacheTime);
                filecaches.FileExt = ".cache";
                filecaches.FileSavePath = HttpContext.Current.Server.MapPath("~/Config/Cache/CacheFiles/");

                if (filecaches.CacheProgress(HttpContext.Current, IsReCache, IsRewritePage))
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return false;
        }
    }
}